package com.project.solomon.SessionBean;

import java.util.Iterator;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.project.entity.Account;
import com.project.entity.Property;
import com.project.entity.Property_Image;

/**
 * Session Bean implementation class PropForRentBean
 */
@Stateless
@LocalBean
public class PropertyBean implements PropertyBeanRemote {

	@PersistenceContext
	EntityManager em;

	public static final String RemoteJNDIName = PropertyBean.class
			.getSimpleName() + "/remote";
	public static final String LocalJNDIName = PropertyBean.class
			.getSimpleName() + "/local";

	public PropertyBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Property> getAllProp() {
		// TODO Auto-generated method stub
		Query query = em.createNamedQuery("Property.findAllProperty");
		@SuppressWarnings("unchecked")
		List<Property> result = query.getResultList();
		return result;
	}

	@Override
	public List<Property> getAllPropFromAcc(Account acc) throws Exception {
		// TODO Auto-generated method stub
		Query query = em
				.createNamedQuery("Property.findAllPropertyFromAccount");
		query.setParameter("accID", acc.getAccID());
		@SuppressWarnings("unchecked")
		List<Property> result = query.getResultList();
		return result;
	}

	@Override
	public String getRandomIMGPath(String propID) throws Exception {
		// TODO Auto-generated method stub
		Query query = em.createNamedQuery("Property_Image.findIMGPath");
		query.setParameter("propID", propID);
		query.setMaxResults(6);
		@SuppressWarnings("unchecked")
		List<String> result = query.getResultList();
		try {
			if (result.size() >= 1) {
				long i = Math.round(Math.random() * result.size() - 0.5);
				String s = result.get((int) i);
				return s;

			} else
				return "/uploaded/PropertyImages/notfound.jpg";
		} catch (Exception e) {
			e.printStackTrace();
			return "/uploaded/PropertyImages/notfound.jpg";
		}

	}

	@Override
	public List<String> getAllIMGPath(String propID) throws Exception {
		// TODO Auto-generated method stub
		Query query = em.createNamedQuery("Property_Image.findIMGPath");
		query.setParameter("propID", propID);
		query.setMaxResults(6);
		@SuppressWarnings("unchecked")
		List<String> result = query.getResultList();
		
		if (result.size() >= 1) 
			return result;
		else {
			result.add("/uploaded/PropertyImages/notfound.jpg");
			return result;
		}
		
	}
	
	@Override
	public boolean updateProperty(Property prop) throws Exception {
		// TODO Auto-generated method stub
		try {
			em.merge(prop);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public List <Property> requestProperty(String pid) throws Exception{
		Query query = em.createQuery("select prop from Property prop where prop.propID = :propID");
		query.setParameter("propID", pid);
		@SuppressWarnings("unchecked")
		List <Property> result = query.getResultList();
		return result;
	}

	@Override
	public List requestPropertyImages(String pid) {
		Query query = em.createQuery("select pimages from Property_Image pimages where pimages.property.propID = :propID");
		query.setParameter("propID", pid);
		List result = query.getResultList();
		return result;
	}

	@Override
	public void deletePropImage(Property_Image p_i) {
		Property_Image prop = em.find(Property_Image.class, p_i.getImageID());
		em.remove(em.merge(prop));
		
	}

	@Override
	public void updatePropImages(Property_Image p_i_l, Property prop) {
		Property property = em.find(Property.class, prop.getPropID());
		property.getPropertyImages().add(p_i_l);
		

	
		
		
		
	}
	
	

}
